// ==========================================================================
// GFast自动生成controller操作代码。
// 生成日期：2024-01-05 17:57:11
// 生成路径: internal/app/flow/controller/flow_model.go
// 生成人：gfast
// desc:流程模型
// company:云南奇讯科技有限公司
// ==========================================================================

package controller

import (
	"context"
	"errors"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/encoding/gurl"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/api/v1/flow"
	systemApi "github.com/tiger1103/gfast/v3/api/v1/system"
	commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
	"github.com/tiger1103/gfast/v3/internal/app/flow/model"
	"github.com/tiger1103/gfast/v3/internal/app/flow/service"
	systemController "github.com/tiger1103/gfast/v3/internal/app/system/controller"
	systemService "github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/libUtils"
	"github.com/xuri/excelize/v2"
)

type flowModelController struct {
	systemController.BaseController
}

var FlowModel = new(flowModelController)

// List 列表
func (c *flowModelController) List(ctx context.Context, req *flow.FlowModelSearchReq) (res *flow.FlowModelSearchRes, err error) {
	res = new(flow.FlowModelSearchRes)
	res.FlowModelSearchRes, err = service.FlowModel().List(ctx, &req.FlowModelSearchReq)
	return
}

// Export 导出excel
func (c *flowModelController) Export(ctx context.Context, req *flow.FlowModelExportReq) (res *flow.FlowModelExportRes, err error) {
	var (
		r        = ghttp.RequestFromCtx(ctx)
		listData []*model.FlowModelInfoRes
		//表头
		tableHead = []interface{}{"主键", "流程名称", "表单类型", "流程表单类别", "流程描述", "业务表单类别", "排序", "流程状态", "创建时间", "修改时间", "删除时间", "创建人", "修改人"}
		excelData [][]interface{}
		//字典选项处理
		flowModelType     *systemApi.GetDictRes
		flowModelTypeMap  = gmap.New()
		flowModelCate     *systemApi.GetDictRes
		flowModelCateMap  = gmap.New()
		flowFormStatus    *systemApi.GetDictRes
		flowFormStatusMap = gmap.New()
	)
	req.PageNum = 1
	req.PageSize = 500
	//获取字典数据
	flowModelType, err = commonService.SysDictData().GetDictWithDataByType(ctx, "flow_model_type", "")
	if err != nil {
		return
	}
	flowModelCate, err = commonService.SysDictData().GetDictWithDataByType(ctx, "flow_model_cate", "")
	if err != nil {
		return
	}
	flowFormStatus, err = commonService.SysDictData().GetDictWithDataByType(ctx, "flow_form_status", "")
	if err != nil {
		return
	}
	for _, v := range flowModelType.Values {
		flowModelTypeMap.Set(v.DictValue, v.DictLabel)
	}
	for _, v := range flowModelCate.Values {
		flowModelCateMap.Set(v.DictValue, v.DictLabel)
	}
	for _, v := range flowFormStatus.Values {
		flowFormStatusMap.Set(v.DictValue, v.DictLabel)
	}
	excelData = append(excelData, tableHead)
	for {
		listData, err = service.FlowModel().GetExportData(ctx, &req.FlowModelSearchReq)
		if err != nil {
			return
		}
		if listData == nil {
			break
		}
		for _, v := range listData {
			var (
				//单选-表单类型
				flowType interface{}
				//单选-流程表单类别
				flowFormCate string
				//单选-业务表单类别
				flowBusinessCate interface{}
				//单选-流程状态
				status interface{}
				//创建人
				createdBy string
				//修改人
				updatedBy string
			)
			//字典-单选-表单类型
			flowType = flowModelTypeMap.Get(gconv.String(v.FlowType))
			//关联表-单选-流程表单类别
			if v.LinkedFlowFormCate != nil {
				flowFormCate = v.LinkedFlowFormCate.Name
			}
			//字典-单选-业务表单类别
			flowBusinessCate = flowModelCateMap.Get(gconv.String(v.FlowBusinessCate))
			//字典-单选-流程状态
			status = flowFormStatusMap.Get(gconv.String(v.Status))
			//创建人
			if v.CreatedUser != nil {
				createdBy = v.CreatedUser.UserNickname
			}
			//修改人
			if v.UpdatedUser != nil {
				updatedBy = v.UpdatedUser.UserNickname
			}
			dt := []interface{}{
				v.Id,
				v.Name,
				//字典-单选-表单类型
				flowType,
				//关联表-单选-流程表单类别
				flowFormCate,
				v.Remark,
				//字典-单选-业务表单类别
				flowBusinessCate,
				v.Sort,
				//字典-单选-流程状态
				status,
				v.CreatedAt.Format("Y-m-d H:i:s"),
				v.UpdatedAt.Format("Y-m-d H:i:s"),
				v.DeletedAt.Format("Y-m-d H:i:s"),
				//创建人
				createdBy,
				//修改人
				updatedBy,
			}
			excelData = append(excelData, dt)
		}
		req.PageNum++
	}
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.Writer)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("流程模型")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}
func (c *flowModelController) ExcelTemplate(ctx context.Context, req *flow.FlowModelExcelTemplateReq) (res *flow.FlowModelExcelTemplateRes, err error) {
	var (
		r = ghttp.RequestFromCtx(ctx)
		//表头
		tableHead = []interface{}{"流程名称", "表单类型", "流程表单类别", "流程描述", "业务表单类别", "排序", "流程状态", "创建时间", "修改时间", "删除时间", "创建人", "修改人"}
		excelData = [][]interface{}{tableHead}
	)
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.Writer)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("流程模型模板")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}
func (c *flowModelController) Import(ctx context.Context, req *flow.FlowModelImportReq) (res *flow.FlowModelImportRes, err error) {
	err = service.FlowModel().Import(ctx, req.File)
	return
}

// LinkedFlowModelDataSearch 相关连表查询数据
func (c *flowModelController) LinkedFlowModelDataSearch(ctx context.Context, req *flow.LinkedFlowModelDataSearchReq) (res *flow.LinkedFlowModelDataSearchRes, err error) {
	if !systemService.SysUser().AccessRule(ctx, systemService.Context().GetUserId(ctx), "api/v1/flow/flowModel/list") {
		err = errors.New("没有访问权限")
		return
	}
	res = new(flow.LinkedFlowModelDataSearchRes)
	res.LinkedFlowModelDataSearchRes, err = service.FlowModel().LinkedFlowModelDataSearch(ctx)
	return
}

// Get 获取流程模型
func (c *flowModelController) Get(ctx context.Context, req *flow.FlowModelGetReq) (res *flow.FlowModelGetRes, err error) {
	res = new(flow.FlowModelGetRes)
	res.FlowModelInfoRes, err = service.FlowModel().GetById(ctx, req.Id)
	return
}

// Add 添加流程模型
func (c *flowModelController) Add(ctx context.Context, req *flow.FlowModelAddReq) (res *flow.FlowModelAddRes, err error) {
	err = service.FlowModel().Add(ctx, req.FlowModelAddReq)
	return
}

// Edit 修改流程模型
func (c *flowModelController) Edit(ctx context.Context, req *flow.FlowModelEditReq) (res *flow.FlowModelEditRes, err error) {
	err = service.FlowModel().Edit(ctx, req.FlowModelEditReq)
	return
}

// Delete 删除流程模型
func (c *flowModelController) Delete(ctx context.Context, req *flow.FlowModelDeleteReq) (res *flow.FlowModelDeleteRes, err error) {
	err = service.FlowModel().Delete(ctx, req.Ids)
	return
}

func (c *flowModelController) ModelNodeSave(ctx context.Context, req *flow.FlowModelNodeSaveReq) (res *flow.FlowModeNodeSaveRes, err error) {
	err = service.FlowModel().SaveNodeInfo(ctx, req.ModelId, req.Nodes, req.Edges)
	return
}

func (c *flowModelController) GetModelNode(ctx context.Context, req *flow.FlowModelNodeInfoGetReq) (res *flow.FlowModelNodeInfoGetRes, err error) {
	res = new(flow.FlowModelNodeInfoGetRes)
	res.NodeInfoRes, err = service.FlowModel().GetNodeInfo(ctx, req.ModelId)
	return
}
